using System;
using System.Configuration;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;

// Project Server 2007 References
using Microsoft.Office.Project.Server.Library;
using Microsoft.Office.Project.Server.Events;

namespace Microsoft.EPM.Events.TimesheetEvent
{
    public class TSOnUpdated : TimesheetEventReceiver
    {
        #region Constants
        private const string CONN_STR = "Data Source={0};Initial Catalog={1};Integrated Security=True";
        #endregion

        #region Properties
        // Initialize local parameters
        private string dbServer;
        private string dbName;
        private string conn_str;
        private EventEntry eventLog = new EventEntry();
        #endregion

        public TSOnUpdated()
        {
            try
            {
                LoadSettings();
                if (dbServer.Length == 0) throw new Exception("DBServer not specified.");
                if (dbName.Length == 0) throw new Exception("DBName not specified.");
            }
            catch (Exception ex)
            {
                eventLog.WriteEvent(ex.Message + "\nError loading settings in " + MethodBase.GetCurrentMethod().Name);
            }
       }

        public override void OnLineApproving(PSContextInfo contextInfo, TimesheetLinePreApproveEventArgs e)
        {
            base.OnLineApproving(contextInfo, e);
        }

        public override void OnUpdating(PSContextInfo contextInfo, TimesheetPreUpdateEventArgs e)
        {
            base.OnUpdating(contextInfo, e);
        }

        public override void OnSubmitting(PSContextInfo contextInfo, TimesheetPreSubmitEventArgs e)
        {
            base.OnSubmitting(contextInfo, e);
        }

        public override void OnUpdated(PSContextInfo contextInfo, TimesheetPostEventArgs e)
        {
            base.OnUpdated(contextInfo, e);

            QueueApproval(contextInfo.SiteGuid, e.TsUID, contextInfo.Lcid, contextInfo.UserName);
        }

        #region Private Methods
        private void QueueApproval(Guid siteGuid, Guid tsGuid, string lcid, string userName)
        {
            try
            {
                //LoadSettings();
                if (conn_str == null)
                {
                    eventLog.WriteEvent("Connection string is null.");
                    return;
                }
                using (SqlConnection cn = new SqlConnection(conn_str))
                {
                    cn.Open();
                    SqlCommand cmdAdd = new SqlCommand("tsAddTimesheet", cn);
                    cmdAdd.CommandType = CommandType.StoredProcedure;
                    SqlParameter parm = new SqlParameter("@siteGuid", SqlDbType.UniqueIdentifier);
                    parm.Value = siteGuid;
                    cmdAdd.Parameters.Add(parm);
                    parm = new SqlParameter("@tsGuid", SqlDbType.UniqueIdentifier);
                    parm.Value = tsGuid;
                    cmdAdd.Parameters.Add(parm);
                    parm = new SqlParameter("@lcid", SqlDbType.NVarChar, 50);
                    parm.Value = lcid;
                    cmdAdd.Parameters.Add(parm);
                    parm = new SqlParameter("@userName", SqlDbType.NVarChar, 50);
                    parm.Value = userName;
                    cmdAdd.Parameters.Add(parm);
                    cmdAdd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                eventLog.WriteEvent(ex.Message + "\nError in " + MethodBase.GetCurrentMethod().Name);
            }
        }

        private void LoadSettings()
        {
            // Load settings from Microsoft.Office.Project.Server.Eventing.exe.config
            try
            {
                dbServer = ConfigurationManager.AppSettings.Get("DBServer").Trim();
                dbName = ConfigurationManager.AppSettings.Get("DBName").Trim();
                conn_str = string.Format(CONN_STR, dbServer, dbName);
            }
            catch (Exception e)
            {
                eventLog.WriteEvent(string.Format("Could not load settings from Microsoft.Office.Project.Server.Eventing.exe.config: {0}", e.Message));
            }
        }
        #endregion
    }
}
